package com.design.pattern.structuremode.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 结构型模式-组合模式
 * @author yll
 * @version 1.0.0
 * @date 2024/03/21 20：37
 * @desc 组合接口
 * */
public abstract class IComponent {

    private String name;


    public IComponent(String name){
        this.name = name;
        componentList = new ArrayList<>();
    }

    protected List<IComponent> componentList;

    public abstract void add(IComponent component);

    public abstract void remove(IComponent component);

    public void read(int level){
        StringBuffer temp = new StringBuffer("");
        for(int i = 0; i < level; i++){
            temp.append("-");
        }
        for (IComponent component : componentList){

            System.out.println(temp + component.name);
            component.read(level+1);
        }
    }

    public int count(){
        return componentList.size();
    }

    public List<IComponent> getComponentList() {
        return componentList;
    }

    public void setComponentList(List<IComponent> componentList) {
        this.componentList = componentList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
